package com.smartstate.dataManagement.util;


import com.smartstate.dataManagement.entity.Equipment;

/**
 * 模糊比较两个字符串
 */
public class IsEquals {

    /**
     * 模糊比较两个字符串
     * @param target 目标字符串
     * @param source 源字符串
     * @return
     */
    public  static boolean isEquals(String target,String source){
        int[] ints = getkmpNext(target);
        int kmp = kmp(target,source, ints);
        if (kmp != -1) {
            return true;
        }
        return false;
    }











    /**
     * 模糊比较两个字符串  返回-1，比较失败，返回别的，比较成功
     *
     * @param str1
     * @param str2
     * @param next
     * @return
     */
    static int  kmp(String str1, String str2, int[] next) {
        int i1 = -1;
        for (int i = 0, j = 0; i < str1.length(); ++i) {

            while (j > 0 && str1.charAt(i) != str2.charAt(j)) {
                j = next[j - 1];
            }

            if (str1.charAt(i) == str2.charAt(j)) {
                j++;
            }

            if (j == str2.length()) {
                return i - j + 1;
            }
        }
        return -1;
    }


    /**
     * @return dest的部分匹配表
     */
    private static int[] getkmpNext(String dest) {
        int length = dest.length();
        int[] next = new int[length];
        next[0] = 0;

        for (int i = 1, j = 0; i < length; i++) {

            while (j > 0 && dest.charAt(i) != dest.charAt(j)) {
                j = next[j - 1];
            }

            if (dest.charAt(i) == dest.charAt(j)) {
                j++;
            }
            next[i] = j;
        }
        return next;
    }
}
